/*--------------------------------------------------------------------
 * File name: mainwindow.c
 * Author: 刘长达      Version:   1.0     Date:
 * Description: 主要为实现相应功能的函数定义，交互等功能的具体实现
 * Contents:
 *                GUI界面
 *                交互功能
 *                函数的具体实现
 *--------------------------------------------------------------------*/

#include "mainwindow.h"
#include "ui_mainwindow.h"
int MainWindow::depth_count = 0;
int MainWindow::seabed_count = 0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //datacorrect(mbparse);
    ui->setupUi(this); //构建槽函数和信号
    connect(ui->actionParese_All,&QAction::triggered,this,&MainWindow::slot_actionParseall_triggered);
    connect(ui->button_depth,&QPushButton::clicked,this,&MainWindow::slot_buttondepth_clicked);
    connect(ui->button_seabed,&QPushButton::clicked,this,&MainWindow::slot_buttonseabed_clicked);
    connect(ui->button_front,&QPushButton::clicked,this,&MainWindow::slot_buttonfront_clicked);
    connect(ui->button_next,&QPushButton::clicked,this,&MainWindow::slot_buttonnext_clicked);
    connect(ui->actionBeamFile,&QAction::triggered,this,&MainWindow::slot_actionBeamLLS_triggered);
    connect(ui->actionSVM,&QAction::triggered,this,&MainWindow::slot_actionSVM_triggered);
    connect(ui->actionKmeans,&QAction::triggered,this,&MainWindow::slot_actionKmeans_triggered);
    // 创建chart等
}

MainWindow::~MainWindow()
{
    delete ui;
}

/* Name: slot_actionParseall_triggered()
 * Function:用以菜单项Depth点击信号触发的槽函数，
 *               完成文件选择对话框、调用parse类完成文件解析
 *               完成文件输出和保存
 * Inpute Paraments: NONE
 * Outpute Paraments: NONE
 * Return: NONE
 * How:
 *            QFileDialog用以打开文件对话框
 *            之后创建mb_parse_binary类的对象，调用其读取和写入函数
 *
 */
void MainWindow::slot_actionParseall_triggered()
{
    QString readfile = QFileDialog::getOpenFileName(this,tr("Choose File"),"d://");
    QString writefile = QFileDialog::getSaveFileName(this, tr("Save  Raw File"), "d://","TEXT(*.txt)");
    QString writedepth = QFileDialog::getSaveFileName(this, tr("Save  Depth File"), "d://","TEXT(*.txt)");
    //QString writeseabed = QFileDialog::getSaveFileName(this, tr("Save  Depth File"), "d://","TEXT(*.txt)");
   // QString writeattitude = QFileDialog::getSaveFileName(this, tr("Save  Attitude File"), "d://","TEXT(*.txt)");
    //QString writeposition = QFileDialog::getSaveFileName(this, tr("Save  Position File"), "d://","TEXT(*.txt)");
    //QString writefileinterval = QFileDialog::getSaveFileName(this, tr("Save interval File"), "d://","TEXT(*.txt)");
    mbparse.func_get_binary(readfile);
    //mbparse.func_print_seabed(writeseabed);
    //mbparse.func_print_depth(writedepth);
    //mbparse.func_print_attitude(writeattitude);
    //mbparse.func_print_postion(writeposition);
    mbparse.func_print_binary(writefile);
    //mbparse.Correct_Com(writefileinterval);
    ui->File_Edit->setText(readfile);
}

/* Name: slot_actionBeamLLS_triggered
 * Function:
 * Inpute Paraments:
 * Outpute Paraments:
 * Return:
 * How:
 * Debug:
 * Questions:
 *
 */
void MainWindow::slot_actionBeamLLS_triggered()
{
    QString readfile = QFileDialog::getOpenFileName(this,tr("Choose Depth File"),"d://A120//");
    QString readfile2 = QFileDialog::getOpenFileName(this,tr("Choose Attitude File"),"d://A120//");
    QString readfile3 = QFileDialog::getOpenFileName(this,tr("Choose  PositionFile"),"d://A120//");
    QString writefile = QFileDialog::getSaveFileName(this, tr("Save  Interval File"), "d://A120//","TEXT(*.txt)");
    //mbparse.Data_Read_Seabed(readfile);
    mbparse.Data_Read(readfile);
    mbparse.Data_Read_Attitude(readfile2);
    mbparse.Data_Read_Postion(readfile3);
    mbparse.Correct_TS_Position();
    mbparse.Print_Interval(writefile);
    //mbparse.Print_BS(writefile);
    ui->File_Edit->setText(readfile);
}

void MainWindow::slot_actionSVM_triggered() {
    QString readfile = QFileDialog::getOpenFileName(this,tr("Choose SVM-Train File"),"d://A120//");
    const string writefile= "d://A120//svmmodel.txt";
    ui->File_Edit->setText(readfile);
    mbparse.trainSVM(readfile,writefile);
    QString testfile = QFileDialog::getOpenFileName(this,tr("Choose SVM-Test File"),"d://A120//");
    mbparse.predictSVM(testfile,writefile);
    ui->File_Edit->setText(testfile);
}

void MainWindow::slot_actionKmeans_triggered()
{
    QString readfile = QFileDialog::getOpenFileName(this,tr("Choose Kmeans File"),"d://A120//");
    QString writefile = QFileDialog::getSaveFileName(this, tr("Save  Depth File"), "d://A120//","TEXT(*.txt)");
    unsigned int size =22538;
    const int dim = 4;   //Dimension of feature
    const int cluster_num = 4; //Cluster number
    const int iteriation = 100; //Cluster number
    mbparse.Kmeans_Cluster(readfile,dim,size,cluster_num,iteriation,writefile);
    ui->File_Edit->setText(readfile);
}

/* Name: slot_buttonfront_clicked()
 * Function: 与用户交互，完成动态显示功能,用来处理按钮按下的信号
 * Inpute Paraments: None
 * Outpute Paraments:
 * Return:
 * How:
 * Debug:
 * Questions:
 *
 */
void MainWindow::slot_buttondepth_clicked()
{
    int i = MainWindow::depth_count;
    status_depth_seabed = true;
    ui->Time_Edit->setText(QString::number(mbparse.depth_arr[i].depth_date));
    ui->depth_Edit->setText(QString::number(mbparse.depth_arr[i].depth_transmit_N[1]));
    ui->ping_Edit->setText(QString::number(mbparse.depth_arr[i].depth_ping_count));
    int max_temp=-32768; int min_temp=65534;
    for (unsigned int count = 0;count < mbparse.depth_arr[i].depth_N;count++) {
        QPointF qpointf(count+1, mbparse.depth_arr[i].depth_120and300_transmit_N[count]);
        if(max_temp<mbparse.depth_arr[i].depth_120and300_transmit_N[count])
            max_temp=mbparse.depth_arr[i].depth_120and300_transmit_N[count];
        if(min_temp>mbparse.depth_arr[i].depth_120and300_transmit_N[count])
            min_temp=mbparse.depth_arr[i].depth_120and300_transmit_N[count];
        point_depth.append(qpointf);
    }
    scatterseries->replace(point_depth);
    lineseries->replace(point_depth);
    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(2);
    pen.setColor(QColor(21, 100, 255));
    lineseries->setPen(pen);//折现序列的线条设置
    scatterseries->setMarkerShape(QScatterSeries::MarkerShapeCircle);//圆形的点
    scatterseries->setBorderColor(QColor(21, 100, 255)); //离散点边框颜色
    scatterseries->setBrush(QBrush(QColor(21, 100, 255)));//离散点背景色
    scatterseries->setMarkerSize(6); //离散点大小
    chart->addSeries(lineseries);
    chart->addSeries(scatterseries);
    chart->setTitle("Depth count charts");
    chart->createDefaultAxes();
    chart->axisX()->setRange(0, mbparse.depth_arr[i].depth_N);
    chart->axisY()->setRange(min_temp,max_temp);
    ui->imagewidget->setChart(chart);
}

void MainWindow::slot_buttonfront_clicked()
{
    if(status_depth_seabed == true) {
        if(depth_count == 0)
            QMessageBox::about(NULL, "About", "This is the first already");
        else {
            for (unsigned int count = 0;count < mbparse.depth_arr[depth_count].depth_N;count++)
                point_depth.removeAt(0);
        depth_count--;
        slot_buttondepth_clicked();
        }
    }
    else {
        if(seabed_count == 0)
            QMessageBox::about(NULL, "About", "This is the first already");
        else {
            for (unsigned int count = 0;count < mbparse.seabed_arr[seabed_count].count_NS;count++)
                point_seabed.removeAt(0);
            seabed_count--;
            slot_buttonseabed_clicked();
        }
    }
}

void MainWindow::slot_buttonseabed_clicked()
{
    int i = MainWindow::seabed_count;
    status_depth_seabed = false;
    ui->Time_Edit->setText(QString::number(mbparse.seabed_arr[i].seabed_date));
    ui->depth_Edit->setText(QString::number(mbparse.depth_arr[i].depth_transmit_N[1]));
    ui->ping_Edit->setText(QString::number(mbparse.seabed_arr[i].seabed_count));

    for (unsigned int count = 0;count < mbparse.seabed_arr[i].count_NS;count++) {
        QPointF qpoints(count+1, mbparse.seabed_arr[i].seabed_ampulitudes_NS[count]);
        point_seabed.append(qpoints);
    }
    scatterseries->replace(point_seabed);
    scatterseries->setMarkerShape(QScatterSeries::MarkerShapeCircle);//圆形的点
    scatterseries->setBorderColor(QColor(21, 100, 255)); //离散点边框颜色
    scatterseries->setBrush(QBrush(QColor(21, 100, 255)));//离散点背景色
    scatterseries->setMarkerSize(6); //离散点大小
    chart->addSeries(scatterseries);
    chart->setTitle("Seabed image charts");
    chart->createDefaultAxes();
    chart->axisX()->setRange(0, mbparse.seabed_arr[i].count_NS);
    chart->axisY()->setRange(-128,-15);
    ui->imagewidget->setChart(chart);
}

void MainWindow::slot_buttonnext_clicked()
{
    if(status_depth_seabed == true) {
        for (unsigned int count = 0;count < mbparse.depth_arr[depth_count].depth_N;count++)
            point_depth.removeAt(0);
        depth_count++;
        slot_buttondepth_clicked();
    }
    else {
        for (unsigned int count = 0;count < mbparse.seabed_arr[seabed_count].count_NS;count++)
            point_seabed.removeAt(0);
        seabed_count++;
        slot_buttonseabed_clicked();
    }
}

/* Name: slot_point_hoverd 需修改
 * Function: 完成散点动态显示数据，移走鼠标消失的功能
 * Inpute Paraments:
 * Outpute Paraments:
 * Return:
 * How:
 * Debug:
 * Questions:
 *
 */
void MainWindow::slot_point_hoverd(const QPointF, bool states)
{
//    if (states) {
//        QLabel *m_valueLabel;
//        m_valueLabel->setText(QString::asprintf("%1.0f%", point.y()));
//        QPoint curPos = mapFromGlobal(QCursor::pos());
//        m_valueLabel->move(curPos.x() - m_valueLabel->width() / 2, curPos.y() - m_valueLabel->height() * 1.5);//移动数值
//        m_valueLabel->show();//显示出来    }
//    else
//        m_valueLabel->hide();//进行隐藏

}
